package com.simdo.g768.Util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.simdo.g768.ZApplication;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("WorldReadableFiles")
public class PrefUtil {

	private final Context mContext;
	private static PrefUtil instance;
	private SharedPreferences APP_PREF;

	public static PrefUtil build() {
		synchronized (PrefUtil.class) {
			if (instance == null) {
				instance = new PrefUtil();
			}
		}
		return instance;
	}

	public PrefUtil() {
		super();
		mContext = ZApplication.getInstance().getContext();
		initPreferences();
	}
	public void initPreferences() {
		APP_PREF = mContext.getSharedPreferences("APP_PREF", Context.MODE_PRIVATE);
	}

	public void putString(String key, String value){
		SharedPreferences.Editor editor = APP_PREF.edit();
		editor.putString(key, value);
		editor.apply();
	}

	public String getString(String key, String def){
		return APP_PREF.getString(key, def);
	}

	public void putInt(String key, int value){
		SharedPreferences.Editor editor = APP_PREF.edit();
		editor.putInt(key, value);
		editor.apply();
	}

	public int getInt(String key, int def){
		return APP_PREF.getInt(key, def);
	}

	public void putBoolean(String key, boolean value){
		SharedPreferences.Editor editor = APP_PREF.edit();
		editor.putBoolean(key, value);
		editor.apply();
	}

	public boolean getBoolean(String key, boolean def){
		return APP_PREF.getBoolean(key, def);
	}

	public void setDropImsiList(List<String> imsiList) {
		StringBuilder imsi = new StringBuilder();
		for (int i = 0; i < imsiList.size(); i++) {
			imsi.append(imsiList.get(i));
			imsi.append(";");
		}
		SharedPreferences.Editor editor = APP_PREF.edit();
		editor.putString("drop_imsi", imsi.toString());
		editor.apply();
	}

	public List<String> getDropImsiList() {
		String imsi = APP_PREF.getString("drop_imsi", null);

		List<String> imsiList = new ArrayList<>();
		if (imsi != null && !imsi.isEmpty()) {
			String[] data = imsi.split(";");
			for (String datum : data) {
				if (!datum.isEmpty()) {
					imsiList.add(datum);
				}
			}
		}
		return imsiList;
	}
}